package com.mzy.linear_struct.hot100.链表;

/**
 * @Author: mizy3
 * @Date: 2024/08/10/下午9:34
 * @Description:
 */
public class Pro234回文链表 {

    public boolean isPalindrome(ListNode head) {
        // 1. 获取中点
        ListNode slow = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        // 获取中点下一个元素
        // 从slow开始反转
        ListNode prev = null;
        ListNode cur = slow;

        while (cur != null) {
            ListNode next = cur.next;
            cur.next = prev;
            prev = cur;
            cur = next;
        }

        // 开始一一比较
        while (prev != null && head != null) {
            if(prev.val != head.val) {
                return false;
            }
            prev = prev.next;
            head = head.next;
        }

        return true;
    }
}





















